//
//  YWNewPostTroubleBillCell.m
//  maintain
//
//  Created by 郑想 on 16/7/23.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWPostTroubleBillCell.h"
#import "GropPictureView.h"
#import "YWHandler.h"
#import "YWCirculationInfo.h"
#import "YWCommonTool.h"
#import "YWMissionManager.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWReportTroubleModel.h"
#import "YWLocation.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionModel.h"
#import "YWReportTroubleManager.h"
#import "YWEquipmentType.h"
#import "YYLabel.h"

@interface YWPostTroubleBillCell()

/** 报障人static */
@property (nonatomic, strong) YYLabel *staticPostTrouble;
@property (nonatomic, strong) UIView *letfLine;
@property (nonatomic, strong) UIView *rightLine;
/** 报障人 */
@property (nonatomic, strong) YYLabel *postTroubleNameLabel;
/** 报障人电话 */
@property (nonatomic, strong) YYLabel *postTroubleMobileLabel;
/** 报障时间 */
@property (nonatomic, strong) YYLabel *postTroubleTimeLabel;
/** 设备名 */
@property (nonatomic, strong) YYLabel *equipmentNameLabel;
/** 位置名 */
@property (nonatomic, strong) YYLabel *positionLabel;
/** 说明 */
@property (nonatomic, strong) YYLabel *noteLabel ;
/** 图片集 */
@property (nonatomic, strong) GropPictureView *picturesView;
/** 图片高的约束 */
@property (nonatomic, strong) MASConstraint *picHeightConstraint;
@end

@implementation YWPostTroubleBillCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    YWPostTroubleBillCell *cell = [tableView dequeueReusableCellWithIdentifier:YWPostTroubleBillCellIdentifier];
    if (cell == nil) {
        cell = [[YWPostTroubleBillCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWPostTroubleBillCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)setTroubleMissionModel:(YWTroubleMissionModel *)troubleMissionModel{
    _troubleMissionModel = troubleMissionModel;
    NSString *timeStr = nil;
    NSArray *pictures = nil;
    
    NSString *reportName = _troubleMissionModel.trouble.channel == YWPostTroubleTypeEwinApp ? (_troubleMissionModel.trouble.reporter.userName ?: @"无") : (_troubleMissionModel.trouble.troubleContact.contactName?:@"无");
    _postTroubleNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障人: " firstColor:[UIColor customTextGrayBlackColor] secondText:reportName.length?reportName:@"无" secondColor:[UIColor blackColor] font:kContentFont];

    NSString *mobile = _troubleMissionModel.trouble.channel == YWPostTroubleTypeEwinApp ? _troubleMissionModel.trouble.reporter.mobile : _troubleMissionModel.trouble.troubleContact.contactNo;
    _postTroubleMobileLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"电话: " firstColor:[UIColor customTextGrayBlackColor] secondText:mobile.length?mobile:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
    timeStr = [YWCommonTool showTimeWithNormalFormatter:_troubleMissionModel.trouble.reportTime / 1000];
    _postTroubleTimeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障时间: " firstColor:[UIColor customTextGrayBlackColor] secondText:timeStr?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    pictures = _troubleMissionModel.trouble.pictures;
    if (pictures.count==0) {
        pictures = [[YWMissionManager shareManager] picturesWithTroubleMissionId:_troubleMissionModel.trouble.troubleId pictureTroubleType:YWPictureTroubleTypeMission];
    }
    
    NSString *equipmentName = nil;
    YWEquipment* equipment = _troubleMissionModel.trouble.equipment;

    if ([equipment.equipmentName isEqualToString:@"位置报障"]){
        equipmentName = @"无";
    }else if (equipment) {
        equipmentName = equipment.equipmentName?:@"无";
    }else if (_troubleMissionModel.trouble.equipmentType){
        equipmentName = _troubleMissionModel.trouble.equipmentType.equipmentTypeName?:@"无";// [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:_troubleMission.equipmentTypeId];
    }
    _equipmentNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障设备: " firstColor:[UIColor customTextGrayBlackColor] secondText:equipmentName.length?equipmentName:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    NSString *location = nil;
    if (equipment) {
        location = [[YWBuildingManager shareManager] equipmentLocation:equipment] ?: @"无";
    }else{
        location = [[YWBuildingManager shareManager] areaLocationWithLocationId:_troubleMissionModel.trouble.location.locationId];
    }
    
    
    _positionLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:location.length?location:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString * noteText = _troubleMissionModel.trouble.note?:@"无";
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障说明: " firstColor:[UIColor customTextGrayBlackColor] secondText:noteText secondColor:[UIColor blackColor] font:kContentFont];
    
    [_picturesView initWithGroupItems:pictures withConstraint:nil];
    
    [self layoutWhenSet]; 
}

- (void)setReportTrouble:(YWReportTroubleModel *)reportTrouble{
    _reportTrouble = reportTrouble;
    NSString *timeStr = nil;
    NSArray *pictures = nil;
    
    NSString *reportName = reportTrouble.channel == YWPostTroubleTypeEwinApp ? (reportTrouble.reporter.userName ?: @"无") : (reportTrouble.troubleContact.contactName?:@"无");
    _postTroubleNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障人: " firstColor:[UIColor customTextGrayBlackColor] secondText:reportName.length?reportName:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    NSString *mobile = reportTrouble.channel == YWPostTroubleTypeEwinApp ? reportTrouble.reporter.mobile : reportTrouble.troubleContact.contactNo;

    _postTroubleMobileLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"电话: " firstColor:[UIColor customTextGrayBlackColor] secondText:mobile.length?mobile:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
    timeStr = [YWCommonTool showTimeWithNormalFormatter:_reportTrouble.reportTime / 1000];
    _postTroubleTimeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障时间: " firstColor:[UIColor customTextGrayBlackColor] secondText:timeStr?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    pictures = _reportTrouble.pictures;
    if (pictures.count==0) {
        pictures = [[YWMissionManager shareManager] picturesWithTroubleMissionId:_reportTrouble.troubleId pictureTroubleType:YWPictureTroubleTypeMission];
    }
    
    NSString *equipmentName = nil;
//    YWEquipment* equipment = _reportTrouble.equipmentId ? [[YWEquipmentManager shareManager] equipmentWithId:_reportTrouble.equipmentId] : nil;
    
    if ([_reportTrouble.equipment.equipmentName isEqualToString:@"位置报障"]) {
        equipmentName = @"无";
    }else if (_reportTrouble.equipment) {
        equipmentName = _reportTrouble.equipment.equipmentName?:@"无";
    }else if (_reportTrouble.equipmentType){
        equipmentName = _reportTrouble.equipmentType.equipmentTypeName?:@"无";//[[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:_troubleMission.equipmentTypeId];
    }

    _equipmentNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障设备: " firstColor:[UIColor customTextGrayBlackColor] secondText:equipmentName.length?equipmentName:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    NSString *location = nil;
//    if (_reportTrouble.equipment) {
//        location = [[YWBuildingManager shareManager] equipmentLocation:_reportTrouble.equipment] ?: @"无";
//    }else{
//        location = [[YWBuildingManager shareManager] areaLocationWithLocationId:_reportTrouble.location.locationId];
//    }
    
    location = [[YWBuildingManager shareManager] areaLocationWithLocationId:_reportTrouble.location.locationId];
    _positionLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:location.length?location:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障说明: " firstColor:[UIColor customTextGrayBlackColor] secondText:_reportTrouble.note?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    [_picturesView initWithGroupItems:pictures withConstraint:nil];
    
    [self layoutWhenSet];
}

- (void)setTroubleRecord:(YWEquipmentTroubleRecord *)troubleRecord{
    _troubleRecord = troubleRecord;
    NSString *timeStr = nil;
    NSArray *pictures = nil;
    YWReportTroubleModel * reportTrouble = [[YWReportTroubleManager shareManager] getOneReportTroubleWithTroubleId:_troubleRecord.troubleId];
    
    NSString *reporter = reportTrouble.reporter.userName.length>0?reportTrouble.reporter.userName:@"无";
    _postTroubleNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障人: " firstColor:[UIColor customTextGrayBlackColor] secondText:reporter secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString *mobile = reportTrouble.reporter.mobile.length?reportTrouble.reporter.mobile:@"无";
    _postTroubleMobileLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"电话: " firstColor:[UIColor customTextGrayBlackColor] secondText:mobile secondColor:[UIColor customYellowColor] font:kContentFont];
    timeStr = [YWCommonTool showTimeWithNormalFormatter:_troubleRecord.reportTime / 1000];
    _postTroubleTimeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障时间: " firstColor:[UIColor customTextGrayBlackColor] secondText:timeStr?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    pictures = [[YWMissionManager shareManager] picturesWithTroubleMissionId:troubleRecord.missionId pictureTroubleType:YWPictureTroubleTypeMission];
    
    NSString *equipmentName = nil;
    YWEquipment* equipment = nil;
    if (troubleRecord.equipmentId) {
        equipment = [[YWEquipmentManager shareManager] equipmentWithId:troubleRecord.equipmentId];
        equipmentName = equipment.equipmentName;
    }else if (troubleRecord.equipmentTypeId){
        equipmentName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:troubleRecord.equipmentTypeId];
    }
    _equipmentNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障设备: " firstColor:[UIColor customTextGrayBlackColor] secondText:equipmentName.length?equipmentName:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString *positionStr = [[YWBuildingManager shareManager] equipmentLocation:equipment]?:@"无";
    _positionLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:positionStr secondColor:[UIColor blackColor] font:kContentFont];
    NSString* troubleDescription = _troubleRecord.reportNote.length >0? _troubleRecord.reportNote : @"无";
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"报障说明: " firstColor:[UIColor customTextGrayBlackColor] secondText:troubleDescription secondColor:[UIColor blackColor] font:kContentFont];
    [_picturesView initWithGroupItems:pictures withConstraint:nil];
    
    [self layoutWhenSet];
}

- (void)layoutWhenSet{
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.postTroubleNameLabel.preferredMaxLayoutWidth = self.postTroubleNameLabel.frame.size.width;
    self.postTroubleMobileLabel.preferredMaxLayoutWidth = self.postTroubleMobileLabel.frame.size.width;
    self.postTroubleTimeLabel.preferredMaxLayoutWidth = self.postTroubleTimeLabel.frame.size.width;
    self.positionLabel.preferredMaxLayoutWidth = self.positionLabel.frame.size.width;
    self.equipmentNameLabel.preferredMaxLayoutWidth = self.equipmentNameLabel.frame.size.width;
}

- (void)setUpSubviews {
    UIView *superview = self.contentView;
    /**
     *  ------ 报障单 -------
     */
    [self.staticPostTrouble mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(superview);
        make.top.mas_equalTo(superview).offset(8);
    }];
    [self.letfLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(_staticPostTrouble.mas_left).offset(-8);
    }];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(@[_staticPostTrouble,_letfLine]);
        make.left.mas_equalTo(_staticPostTrouble.mas_right).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
        make.height.mas_equalTo(@[@1,_letfLine]);
    }];
    
    /**
     *  报障人
     *  报障时间
     *  设备
     *  位置
     *  说明
     *  图片集
     */
    [self.postTroubleNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_staticPostTrouble.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    
    [self.postTroubleMobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_postTroubleNameLabel.mas_bottom).offset(6);
    }];
    
    [self.postTroubleTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_postTroubleMobileLabel.mas_bottom).offset(6);
    }];
    
    [self.equipmentNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_postTroubleTimeLabel.mas_bottom).offset(6);
        
    }];
    
    [self.positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_equipmentNameLabel.mas_bottom).offset(6);
    }];
    
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_positionLabel.mas_bottom).offset(6);
    }];
    
    [self.picturesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6).priorityHigh();
        make.left.and.right.mas_equalTo(@[_postTroubleNameLabel,_postTroubleMobileLabel,_postTroubleTimeLabel,_equipmentNameLabel,_positionLabel,_noteLabel]);
        _picHeightConstraint = make.height.mas_equalTo(@90).priorityHigh();
        make.bottom.mas_equalTo(superview).priorityHigh();
    }];
}
//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}
- (YYLabel *)staticPostTrouble{
    if (!_staticPostTrouble) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        label.text = @"报障单";
        label.numberOfLines = 1;
        _staticPostTrouble = label;
        [self.contentView addSubview:_staticPostTrouble];
    }
    return _staticPostTrouble;
}

- (UIView *)letfLine{
    if (!_letfLine) {
        _letfLine = [UIView new];
        _letfLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_letfLine];
    }
    return _letfLine;
}

- (UIView *)rightLine{
    if (!_rightLine) {
        _rightLine = [UIView new];
        _rightLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_rightLine];
    }
    return _rightLine;
}

- (YYLabel *)postTroubleNameLabel{
    if (!_postTroubleNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _postTroubleNameLabel = label;
        [self.contentView addSubview:_postTroubleNameLabel];
    }
    return _postTroubleNameLabel;
}

- (YYLabel *)postTroubleMobileLabel{
    if (!_postTroubleMobileLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        label.userInteractionEnabled = YES;
        _postTroubleMobileLabel = label;
        [_postTroubleMobileLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMobileTapEvent:)]];
        [self.contentView addSubview:_postTroubleMobileLabel];
    }
    return _postTroubleMobileLabel;
}

- (YYLabel *)postTroubleTimeLabel{
    if (!_postTroubleTimeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _postTroubleTimeLabel = label;
        [self.contentView addSubview:_postTroubleTimeLabel];
    }
    return _postTroubleTimeLabel;
}

- (YYLabel *)equipmentNameLabel{
    if (!_equipmentNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _equipmentNameLabel = label;
        [self.contentView addSubview:_equipmentNameLabel];
    }
    return _equipmentNameLabel;
}

- (YYLabel *)positionLabel{
    if (!_positionLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _positionLabel = label;
        [self.contentView addSubview:_positionLabel];
    }
    return _positionLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

-(GropPictureView *)picturesView{
    if (!_picturesView) {
        GropPictureView *gropPictureView = [GropPictureView new];
        _picturesView = gropPictureView;
        [self.contentView addSubview:_picturesView];
    }
    return _picturesView;
}

- (void)handleMobileTapEvent:(id)sender{
    if ([self.delegate respondsToSelector:@selector(clickPostTroubleMobileWithNumber:)]) {
        NSString *mobile = nil;
        if (_troubleMissionModel) {
            mobile = _troubleMissionModel.trouble.channel == YWPostTroubleTypeEwinApp ? _troubleMissionModel.trouble.reporter.mobile : _troubleMissionModel.trouble.troubleContact.contactNo;
        }else if (_troubleRecord){
            mobile = _troubleRecord.reporterNumber;
        }else{
            mobile = _reportTrouble.channel == YWPostTroubleTypeEwinApp ? _reportTrouble.reporter.mobile : _reportTrouble.troubleContact.contactNo;
        }
        [self.delegate clickPostTroubleMobileWithNumber:mobile];
    }
}

@end
